//
//  EmotitionViewModel.swift
//  表情栏
//
//  Created by Dengdwd on 15/10/21.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit

class EmotitionViewModel {

    lazy var emotitionPakages = [EmotitionPackage]()
    
    static let shareEmotitionViewModel = EmotitionViewModel()
    
    private init() {
        
        loadPakages()
        
    }

    func favorriteEmotition(indexPath:NSIndexPath) {
    
        if indexPath.section == 0 {
        return
        }
        let em = emotition(indexPath)
        em.times++
        if !emotitionPakages[0].emoticons.contains(em){
            emotitionPakages[0].emoticons.insert(em, atIndex: 0)
        }
        emotitionPakages[0].emoticons.sortInPlace{ $0.times > $1.times }
        
        if emotitionPakages[0].emoticons.count > 21 {
        emotitionPakages[0].emoticons.removeAtIndex(19)
        }
    }
    
    func emotition(indexPath:NSIndexPath) ->Emotition {
        return emotitionPakages[indexPath.section].emoticons[indexPath.item]
    }
    
    private func loadPakages() {
        emotitionPakages.append(EmotitionPackage(dict: ["group_name_cn":"最近"]))
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle/")
        let dict = NSDictionary(contentsOfFile: path!)
        if let array = (dict!["packages"] as? [[String:AnyObject]]) {
            for dic in array {
            let imagePath = NSBundle.mainBundle().bundlePath + "/Emoticons.bundle/" + (dic["id"] as! String) + "/info.plist"
            let d = NSDictionary(contentsOfFile: imagePath)
            emotitionPakages.append(EmotitionPackage(dict: d! as! [String : AnyObject]))
        
            }
        }
    }


}
